<script setup lang="tsx">
import {
  fetchBatchDeleteTenant,
} from '@/service/api/system/tenant';
import { fetchGetOrdersList } from '@/service/api/business/orders';
import { useAppStore } from '@/store/modules/app';
import { useAuth } from '@/hooks/business/auth';
import { useTable, useTableOperate } from '@/hooks/common/table';
import { useDownload } from '@/hooks/business/download';
import { useDict } from '@/hooks/business/dict';
import DictTag from '@/components/custom/dict-tag.vue';
import { $t } from '@/locales';
import TenantSearch from './modules/tenant-search.vue';

defineOptions({
  name: 'TenantList'
});

useDict('sys_normal_disable');

const appStore = useAppStore();
const { download } = useDownload();
const { hasAuth } = useAuth();
const {
  columns,
  columnChecks,
  data,
  getData,
  getDataByPage,
  loading,
  mobilePagination,
  searchParams,
  resetSearchParams
} = useTable({
  apiFn: fetchGetOrdersList,
  apiParams: {
    pageNum: 1,
    pageSize: 10,
    // if you want to use the searchParams in Form, you need to define the following properties, and the value is null
    // the value can not be undefined, otherwise the property in Form will not be reactive
    id: null,
    username: null,
    orderId: null,
    merchantTransactionId: null,
    productName: null,
    payWay: null,
    createTime: null,
    payStatus: null,
    refund: null
  },
  columns: () => [
    {
      type: 'selection',
      align: 'center',
      width: 48
    },
    {
      key: 'index',
      title: $t('common.index'),
      align: 'center',
      width: 64
    },
    {
      key: 'username',
      title: '代理人',
      align: 'center',
      minWidth: 80
    },
    {
      key: 'productName',
      title: '产品名称',
      align: 'center',
      minWidth: 120
    },
    {
      key: 'payAmount',
      title: '支付金额',
      align: 'center',
      minWidth: 120
    },
    {
      key: 'payWay',
      title: '支付方式',
      align: 'center',
      minWidth: 120
    },
    {
      key: 'orderId',
      title: '订单Id',
      align: 'center',
      minWidth: 120
    },
    {
      key: 'merchantTransactionId',
      title: '商家交易流水号',
      align: 'center',
      minWidth: 120
    },
    {
      key: 'createTime',
      title: '创建时间',
      align: 'center',
      minWidth: 120
    },
    {
      key: 'payTime',
      title: '支付时间',
      align: 'center',
      minWidth: 120
    },
    {
      key: 'payStatus',
      title: '支付状态',
      align: 'center',
      minWidth: 120,
      render(row) {
        const statusValue = Number(row.payStatus);
        console.log('当前行支付状态:', row.payStatus, typeof row.payStatus);
        console.log('完整行数据:', JSON.stringify(row));
        return <DictTag size="small" value={statusValue} dictCode="sys_normal_disable" />;
      }
    },
    {
      key: 'refund',
      title: '退款',
      align: 'center',
      minWidth: 120,
      render(row) {
        return <DictTag size="small" value={row.refund} dictCode="sys_normal_disable" />;
      }
    },
    {
      key: 'operate',
      title: $t('common.operate'),
      align: 'center',
      width: 180,
      render: row => {
        return <div class="flex-center gap-8px"></div>;
      }
    }
  ]
});

const { handleAdd, checkedRowKeys, onBatchDeleted } =
  useTableOperate(data, getData);

async function handleBatchDelete() {
  // request
  const { error } = await fetchBatchDeleteTenant(checkedRowKeys.value);
  if (error) return;
  onBatchDeleted();
}


async function handleExport() {
  download('/system/tenant/export', searchParams, `租户列表_${new Date().getTime()}.xlsx`);
}
</script>

<template>
  <div class="min-h-500px flex-col-stretch gap-16px overflow-hidden lt-sm:overflow-auto">
    <TenantSearch v-model:model="searchParams" @reset="resetSearchParams" @search="getDataByPage" />
    <NCard title="订单列表" :bordered="false" size="small" class="card-wrapper sm:flex-1-hidden">
      <template #header-extra>
        <TableHeaderOperation
          v-model:columns="columnChecks"
          :disabled-delete="checkedRowKeys.length === 0"
          :loading="loading"
          :show-add="false"
          :show-delete="false"
          :show-export="hasAuth('system:tenant:export')"
          @add="handleAdd"
          @delete="handleBatchDelete"
          @export="handleExport"
          @refresh="getData"
        >
        </TableHeaderOperation>
      </template>
      <NDataTable
        v-model:checked-row-keys="checkedRowKeys"
        :columns="columns"
        :data="data"
        size="small"
        :flex-height="!appStore.isMobile"
        :scroll-x="962"
        :loading="loading"
        remote
        :row-key="row => row.id"
        :pagination="mobilePagination"
        class="sm:h-full"
      />
    </NCard>
  </div>
</template>

<style scoped></style>
